package cn.yhbk.web.common.Util;

import cn.yhbk.web.common.api.CommonPage;
import cn.yhbk.web.domain.AdminUserDetails;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.cglib.core.Local;
import org.springframework.security.core.context.SecurityContextHolder;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyUtil {

    public static Long getCurrentAdminId(){
        return ((AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUmsAdmin().getId();
    }

    public static Map<String,Object> makeRespPage(Page page, Object myData){
        Map<String, Object> map = new HashMap<>();

        map.put("pageNum",page.getCurrent());
        map.put("pageSize",page.getSize());
        map.put("totalPage",page.getPages());
        map.put("total",page.getTotal());
        map.put("data",myData);
        return map;
    }

    public static String getCurrentYear(){
        return String.valueOf(LocalDate.now().getYear());
    }

    public static boolean checkYear(String year) {

        String regex = "^(?:(?:1\\d{3})|(?:20\\d{2}))$";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(year);

        return matcher.matches();
    }
}
